<?php
namespace app\api\model;
use plugin\filter;
use think\Db;
use think\Model;
use app\api;

/*客服评论*/
class KfAssess extends CommonModel
{
    private $table_name = 'kf_assess';
    public function __construct($args = [])
    {
        parent::__construct($args);
    }

    public function defaultParam($args)
    {
        $data = [
            'client_id' => '',
            'kf_id'      => "",
            'cent'   => "5",
            'add_time'  => time()
        ];
        return array_merge($data,$args);
    }

    //新增
    public function add($args)
    {
        $data = $this->defaultParam($args);
        if(empty($data['client_id']))
        {
            return ['error'=> 1 ,'msg'=> '没有指定设置的用户'];
        }
        if(empty($data['kf_id']))
        {
            return ['error'=> 1 ,'msg'=> '没有指定客服'];
        }

        if($this->userHasAssess($data['client_id'],$data['kf_id']))
        {
            return ['error'=> 1 ,'msg'=> '今天已经对此客服评论了'];
        }
        $data['cent'] = intval($data['cent']) == 0 || intval($data['cent']) > 6 ? 5 : intval($data['cent']);

        $data = resetArr($data,['client_id','kf_id','cent','add_time']);

        db($this->table_name)->insertGetId($data);

        return ['error'=>0,'msg'=>"评分完毕"];
    }

    //用户是否已经评价
    public function userHasAssess($client_id,$kf_id)
    {
        $assess = db($this->table_name)->where(['client_id'=>$client_id,'kf_id'=>$kf_id])->order('id DESC')->find();

        //今天内已经评论
        return !empty($assess) && date('Y-m-d',time()) == date('Y-m-d',$assess['add_time']);
    }


}